package com.neuedu.yyzx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.yyzx.common.Ret;
import com.neuedu.yyzx.po.Customer;
import com.neuedu.yyzx.po.Outward;
import com.neuedu.yyzx.service.CustomerService;
import com.neuedu.yyzx.service.OutwardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author
 * 项目：yyzx-project
 * site:http://...
 * 描述：外出登记 前端页面 outward 数据库 outward
 * 思路：
 * @data 2025/4/10/11:26
 */
@RestController
@RequestMapping("/outward")
public class OutwardController {
    @Autowired
    private OutwardService outwardService;

    @Autowired
    private CustomerService customerService;

    // http://localhost:8080/outward/customer/page
    // 外出登记左侧 客户查询
    @GetMapping("customer/page")
    public Ret<List<Customer>> page(Page<Customer> page, Customer customer) {
        // 创建LambdaQueryWrapper对象，用于后续的条件构造
        LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();

        // 如果用户昵称不为空，则添加昵称模糊查询条件
        if (customer.getCustomerName() != null) {
            queryWrapper.like(Customer::getCustomerName, customer.getCustomerName());
        }
        if (customer.getRoomNo() != null) {
            queryWrapper.like(Customer::getRoomNo, customer.getRoomNo());
        }
        // 添加按创建时间降序排序的条件
        queryWrapper.orderByDesc(Customer::getCheckinDate);

        // 调用CustomerService的分页查询方法，并返回查询结果
        return Ret.ok(customerService.page(page, queryWrapper));
    }

    // http://localhost:8080/outward/selectListWithCustomerName
    // 外出登记右侧 外出信息查询（显示姓名）
    @GetMapping("/selectListWithCustomerName")
    public Ret<Page<Outward>> selectListWithCustomerName(Page<Outward> page, Outward outward) {  // 传的对象信息(看需要传输多少个数据 如果是多个用对象方便)
        return Ret.ok(outwardService.selectListWithCustomerName(page, outward));
    }

    // 保存或更新用户信息
    @PostMapping("/saveOrUpdate")
    // 前段的axios传递的json数据，需要使用@RequestBody注解
    public Ret<Boolean> saveOrUpdate(@RequestBody Outward outward) {
        boolean saveOrUpdate = outwardService.saveOrUpdate(outward);
        return Ret.ok(saveOrUpdate);
    }

    // http://localhost:8080/outward/get/1
    // 根据主键查询 前段用于修改信息
    @GetMapping("/get/{id}")
    public Ret<Customer> getById(@PathVariable int id) {
        Outward outward = outwardService.getById(id);
        return Ret.ok(outward);
    }

    // http://localhost:8080/outward/delete/1
    // 根据主键删除
    @DeleteMapping("/delete/{id}")
    public Ret<Boolean> removeById(@PathVariable int id) {
        return Ret.ok(outwardService.removeById(id));
    }

    // http://localhost:8080/outward/updateAuditStatus/1
    // 修改外出审批状态
    @GetMapping("/updateAuditStatus/{outwardId}/{status}")
    public Ret<Boolean> updateAuditStatus(@PathVariable int outwardId, @PathVariable int status) {
        Outward outward = outwardService.getById(outwardId);
        outward.setAuditstatus(status);
        return Ret.ok(outwardService.updateById(outward));
    }

    // http://localhost:8080/outward/updateAuditStatus/1
    // 登记回院时间
    @GetMapping("/returnTime")
    public Ret<Boolean> returnTime(Outward paramOutward) {
        Outward outward = outwardService.getById(paramOutward.getOutwardId());
        System.out.println("outward = " + outward);
        outward.setActualreturntime(paramOutward.getActualreturntime());
        return Ret.ok(outwardService.updateById(outward));
    }

    // http://localhost:8080/outward/page
    // 外出登记分页查询 无法显示 姓名
    @GetMapping("/page")
    public Ret<List<Outward>> page(Page<Outward> page, Outward outward) {
        // 创建LambdaQueryWrapper对象，用于后续的条件构造
        LambdaQueryWrapper<Outward> queryWrapper = new LambdaQueryWrapper<>();
        // // 如果用户昵称不为空，则添加昵称模糊查询条件
        // if (outward.getCustomerId() != null) {
        //     queryWrapper.like(Outward::getCustomerId, outward.getCustomerId());
        // }

        // 添加按需要的排序条件
        queryWrapper.orderByDesc(Outward::getOutwardId);

        // 调用CustomerService的分页查询方法，并返回查询结果
        return Ret.ok(outwardService.page(page, queryWrapper));
    }

}
